package com.zhongke.cotrun.controller;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.pojo.DictEntity;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.model.validator.UserValidator;
import com.zhongke.cotrun.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 数据字典表
 *
 * @author lee
 * @email wawzj512541@gmail.com
 * @date 2019-10-10 09:41:28
 */
@Validated
@RestController
@RequestMapping("/dict")
public class DictController {
    
    @Autowired
    private DictService dictService;

    /**
     * 新增数据字典
     * @param entity    实体类
     * @return APIResponse
     */
    @PostMapping("/addDict")
    public APIResponse addDict(@Validated({UserValidator.InSet.class}) DictEntity entity){
        if(dictService.add(entity)){
            return APIResponse.returnSuccess("操作成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 修改数据字典
     * @param entity    实体类
     * @return APIResponse
     */
    @PostMapping("/updateById")
    public APIResponse updateById(@Validated({UserValidator.UpDate.class})DictEntity entity){
        if(dictService.update(entity)){
            return APIResponse.returnSuccess("操作成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 获取所有字典元素
     * @return APIResponse
     */
    @GetMapping("/selectAll")
    public APIResponse selectAll(){
        List<DictEntity> list = dictService.getAll();
        if(list.size()>0){
            return APIResponse.returnSuccess(list);
        }else{
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 根据父id获取字典元素
     *
     * @param parentId 父id
     * @return APIResponse
     */
    @GetMapping("/getDictByParentId")
    public APIResponse getByParentId(Integer parentId){
        List<DictEntity> list = dictService.getByParentId(parentId);
        if(list.size()>0){
            return APIResponse.returnSuccess(list);
        }else{
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 根据type获取字典元素列表
     *
     * @param type 类型
     * @return APIResponse
     */
    @GetMapping("/getListByType")
    public APIResponse getListByType(String type){
        List<DictEntity> list = dictService.getListByType(type);
        if(list.size()>0){
            return APIResponse.returnSuccess(list);
        }else{
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 根据type获取字典元素
     *
     * @param type 类型
     * @return APIResponse
     */
    @GetMapping("/getItemByType")
    public APIResponse getItemByType(String type){
        DictEntity entity = dictService.getItemByType(type);
        if(entity!=null){
            return APIResponse.returnSuccess(entity);
        }else{
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 根据type获取字典树
     *
     * @param type 类型
     * @return APIResponse
     */
    @GetMapping("/getTreeByType")
    public APIResponse getTreeByType(String type){
        DictEntity entity = dictService.getTreeByType(type);
        if(entity!=null){
            return APIResponse.returnSuccess(entity);
        }else{
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 逻辑删除
     * @param id 主键id
     * @return APIResponse
     */
    @RequestMapping(value="/deleteById",method = {RequestMethod.POST, RequestMethod.DELETE})
    public APIResponse deleteById(Integer id){
        if(dictService.delById(id)){
            return APIResponse.returnSuccess("操作成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

}
